package com.lyc.common.util;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author 李大神仙
 * @Date 2024/2/28 21:10
 * @Version 1.0
 */
public class RequestUtil {
    public static final String AUTHORIZATION = "authorization";

    public static final String ENCRYPT = "x-encryption-header";

    public static String getHeader(String header){
        return Optional.ofNullable((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).map(at ->
            getHeader(at.getRequest(),header)).orElse("");
    }

    public static String getHeader(HttpServletRequest request,String header){
        return Optional.ofNullable(request).map(req -> req.getHeader(header)).orElse("");
    }

    public static String getAuthorization(){
        return Optional.ofNullable((ServletRequestAttributes)RequestContextHolder.getRequestAttributes())
                .map(at -> getHeader(at.getRequest(),AUTHORIZATION)).orElse("");
    }
}

































